/**
 * ProductAction.java
 * 2008-3-17
 * 作者 Administrator
 */
package org.jackysoft.struts2.business;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.Product;
import org.jackysoft.entity.ProductQuota;
import org.jackysoft.entity.Technics;
import org.jackysoft.report.BadProduct;
import org.jackysoft.service.ProductService;
import org.jackysoft.service.TechnicsService;
import org.jackysoft.struts2.UtilsCRUDAction;
import org.jackysoft.util.HttpUtil;
import org.jackysoft.util.StringUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
@Namespace("/product")
@Results(
		@Result(
	    		name="quotalist",
	    		type="json",
	    		params={
	    				"excludeNullProperties","true",
	    				"ignoreHierarchy","false",
	    				"root","quotas"
	    		}
	    		
	    
		)
)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ProductAction extends UtilsCRUDAction<Product> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2665517861718672211L;
	private ProductService service;
	private TechnicsService techService;
	private Product bean = new Product();
	private Collection<Product> beans;
	private Collection<BadProduct> badBeans;
	private Collection<ProductQuota> quotas;
	private Department department;
	private Collection<ProductQuota> quotabeans;	
	private List<String> errorInfo;

	public String listallcode() throws Exception{
		beans = service.gets();
		return BEANLIST;
	}
	
	public String listcode() throws Exception{
		beans = service.getsTypedOfGrouped(bean.getItemType());		
		return BEANLIST;
	}
	
	public String search() throws Exception{
		beans = service.search(term,department);
		return BEANLIST;
	}
	
	public String searchbad() throws Exception{
		beans  =service.searchbad(bean.getCode());
		return BEANLIST;
	}
	

	public String getone() throws Exception{
		bean = service.get(bean.getCode());
		Collection<Technics> technics = techService.getsByProduct(bean);
		bean.setTechnics(technics);
		return "getone";
	}
	
	public String badcodes() throws Exception{
		badBeans = service.badCodes();
		return "badcode";
	}
	
	public String searchall()throws Exception{
		beans = service.search(term);
		return BEANLIST;
	}
	
	public String queryquotas() throws Exception{
		quotas = service.getsQuotas(term);
		return "quotalist";
	}
	
	public String quotasnode() throws Exception{
		zbeans = service.getProductTree(term,department,bean.getItemType());		
		return ZBEANS;
	}
	
	
	public String createquotas() throws Exception{
		service.createQuota(quotabeans);
	    return SUCCESS;
	}
	
	public String updatequotas() throws Exception{
		service.updateQuota(quotabeans);
		return SUCCESS;
	}
	
	public String removequotas() throws Exception{
		service.removeQuota(quotabeans);
		return SUCCESS;
	}
	
	public String datainput() throws Exception{
	    InputStream input = new FileInputStream(upLoad);
		errorInfo = service.importProducts(input, upLoadFileName); 				
			
	    return "datainput";
	}

	public String dataoutput() throws Exception{
		
		byte[] d = service.exportProducts();
		upLoadFileName = StringUtils.toDownloadFileName("全部产品表格.xls");
		inputStream= new ByteArrayInputStream(d);
		contentType = HttpUtil.extToMimeType("xls");
		contentLength = d.length;
		return DOWNLOAD;
		
	}
	
	public String updatePrice() throws Exception{
		service.updatePrice(upLoad);
		return "updatePrice";
	}
	
	@Resource
	public void setService(ProductService service) {
		this.service = service;
		this.setDataProvider(service);
	}
	
	public ProductService getService() {
		return service;
	}

	public Product getBean() {
		return bean;
	}

	public void setBean(Product bean) {
		this.bean = bean;
	}

	public Collection<Product> getBeans() {
		return beans;
	}

	public void setBeans(Collection<Product> beans) {
		this.beans = beans;
	}



	public Department getDepartment() {
		return department;
	}



	public void setDepartment(Department department) {
		this.department = department;
	}

	public Collection<ProductQuota> getQuotas() {
		return quotas;
	}

	public void setQuotas(Collection<ProductQuota> quotas) {
		this.quotas = quotas;
	}

	public Collection<ProductQuota> getQuotabeans() {
		return quotabeans;
	}

	public void setQuotabeans(Collection<ProductQuota> quotabeans) {
		this.quotabeans = quotabeans;
	}

	public List<String> getErrorInfo() {
		return errorInfo;
	}

	public void setErrorInfo(List<String> errorInfo) {
		this.errorInfo = errorInfo;
	}

	public Collection<BadProduct> getBadBeans() {
		return badBeans;
	}

	public void setBadBeans(Collection<BadProduct> badBeans) {
		this.badBeans = badBeans;
	}

	@Resource
	public void setTechService(TechnicsService techService) {
		this.techService = techService;
	}
}

